$(document).ready(function() {
	$( ".find-container" ).addClass("ui-widget ui-widget-content ui-corner-all");
	$( ".button-find" ).button();
	$.getJSON('assets/data/country.json', function(data){
	    $.each(data.countries, function(index, country){
			$( ".select-div select:first" ).append(
				"<option value='"+country.id+"'>"+country.name+"</option>"
			);
	    });
	}).fail(function(){
		alert("Fail: maybe the country data does not exists or is not json...");
	});
	$.getJSON('assets/data/range.json', function(data){
		$.each(data.ranges, function(index, range){
			$( ".select-div select:last" ).append(
				"<option value='"+range.id+"'>$"+range.minimum +" to $"+range.maximum+"</option>"
			);
		});
	}).fail(function(){
		alert("Fail: maybe the investment range data does not exists or is not json...");
	});
	$.getJSON('assets/data/crop.json', function(data){
		$.each(data.crops, function(index, crop){
				$( ".select-div select:eq(1)" ).append(
					"<option value='"+crop.id+"'>"+crop.description+"</option>"
				);
		});
	}).fail(function(){
		alert("Fail: maybe the crop data does not exists or is not json...");
	});
	var country_select = $( ".select-div select:first" );
	var crop_select = $( ".select-div select:eq(1)" );
	var range_select = $( ".select-div select:last" );
	country_select.combobox();
	crop_select.combobox();
	range_select.combobox();
	var country_text = $( ".select-div .custom-combobox:first .custom-combobox-input" );
	var crop_text = $( ".select-div .custom-combobox:eq(1) .custom-combobox-input" );
	var range_text = $( ".select-div .custom-combobox:last .custom-combobox-input" );
	country_text.val("Select country").attr("name","country");
	crop_text.val("Select crop").attr("name","crop");
	range_text.val("Investment range").attr("name","range");
	function validateFields(){
		var valid = true;
		if(country_text.val()==="Select country" || crop_text.val()==="Select crop" || range_text.val()==="Investment range"){
			valid = false;
		}
		return valid;
	}
	$( ".select-div .custom-combobox:first" ).click(function(e) {
		e.stopPropagation();
		var text = country_text.val();
		if(text==="Select country"){
			country_text.val("");
		}
		if(crop_text.val()===""){
			crop_text.val("Select crop");
		}
		if(range_text.val()===""){
			range_text.val("Investment range");
		}
	});
	$( ".select-div .custom-combobox:eq(1)" ).click(function(e) {
		e.stopPropagation();
		var text = crop_text.val();
		if(text==="Select crop"){
			crop_text.val("");
		}
		if(country_text.val()===""){
			country_text.val("Select country");
		}
		if(range_text.val()===""){
			range_text.val("Investment range");
		}
	});
	$( ".select-div .custom-combobox:last" ).click(function(e) {
		e.stopPropagation();
		var text = range_text.val();
		if(text==="Investment range"){
			range_text.val("");
		}
		if(crop_text.val()===""){
			crop_text.val("Select crop");
		}
		if(country_text.val()===""){
			country_text.val("Select country");
		}
	});
	$('html').click(function() {
		if(crop_text.val()===""){
			crop_text.val("Select crop");
		}
		if(country_text.val()===""){
			country_text.val("Select country");
			crop_text.val("Select crop");
		}
		if(range_text.val()===""){
			range_text.val("Investment range");
		}
	});
	$( ".find-container h2, h4, .button-find" ).hide().show("fade",1000);
	$( ".select-div" ).hide().show("drop",700);
	$( ".button-find" ).click(function(){
		if(validateFields()){
			country_id = country_select.val();
			crop_id = crop_select.val();
			range_id = range_select.val();
			country_name = country_select.find("option:selected").text();
			crop_name = crop_select.find("option:selected").text();
			range = range_select.find("option:selected").text();
			alert('Id: '+country_id+', Country: '+country_name+
				  '\rId: '+crop_id+', Crop: '+crop_name+
				  '\rId: '+range_id+', Range: '+range);
		}else{
			alert("All form fields are required");
		}
	});
});